﻿using productplanModule.ServiceReference1;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace productplanModule.Views
{
    /// <summary>
    /// Interaction logic for AddModule10MainView.xaml
    /// </summary>

     [Export("AddModule10MainView")]
    public partial class AddModule10MainView : UserControl
    {

         ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));
         productplan_put deleteId;
        public AddModule10MainView()
        {

            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //여기에 데이터를 로드하고 결과를 CollectionViewSource에 할당하십시오.
                System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["productplan_putViewSource"];
                myCollectionViewSource.Source = context.productplan_put;
            }

            locationComboBox.Loaded += (sender, e) => ComboReset();

            //locationComboBox.Loaded += (sender, e) => ComboReset();

            EnabledFunc();
        }

        public void ComboReset()
        {
            ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));
           
            var lo = context.Location_table.Select(kkk => kkk);

                locationComboBox.ItemsSource = lo;
                locationComboBox.DisplayMemberPath = "location";

            var pro = context.products.Select(kkk => kkk);


            product_nameComboBox.ItemsSource = pro;
            product_nameComboBox.DisplayMemberPath = "product_name";

            var rere = context.productplan_put.Select(kkk=>kkk);

            DataServiceCollection<productplan_put> lstData = new DataServiceCollection<productplan_put>();
            lstData.LoadCompleted += (senderobj, evtargs) =>
            {
                if (evtargs.Error == null)
                {
                    productplan_putDataGrid.ItemsSource = lstData;
                }
            };

            lstData.LoadAsync(context.productplan_put);

          

        }

        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {

            productplan_put pp = this.productplan_putDataGrid.SelectedItem as productplan_put;

            this.productplanTextBox.Text = pp.productplan;
            this.productplan_codeTextBox.Text = pp.productplan_code;
            this.productplan_insert_dayDatePicker.SelectedDate = pp.productplan_insert_day;
            this.productplan_set_predict_dayDatePicker.SelectedDate = pp.productplan_set_predict_day;
            this.productplan_sub_predict_dayDatePicker.SelectedDate = pp.productplan_sub_predict_day;
            this.product_quantityTextBox.Text = pp.product_quantity.ToString();
            this.locationComboBox.Text = pp.location;
            this.product_nameComboBox.Text = pp.product_name;
            this.work_order_numberTextBox.Text = pp.work_order_number.ToString();
            deleteId =context.productplan_put.Where(kkk => kkk.id == pp.id).SingleOrDefault();
            Init();
            EnabledFunc();

         

        }

      

        public void EnabledFunc()
        {
            this.locationComboBox.SelectionChanged += (sender, e) => this.product_nameComboBox.IsEnabled = true;
            this.product_nameComboBox.SelectionChanged += (sender, e) => this.product_quantityTextBox.IsEnabled = true;
            this.product_quantityTextBox.TextChanged += (sender, e) => this.productplanTextBox.IsEnabled = true;
            //this.productplanTextBox.TextChanged += (sender, e) => this.productplan_codeTextBox.IsEnabled = true;
            this.productplanTextBox.TextChanged += (sender, e) => this.productplan_insert_dayDatePicker.IsEnabled = true;
            //this.productplan_codeTextBox.TextChanged += (snder, e) => this.productplan_insert_dayDatePicker.IsEnabled = true;

            this.productplan_insert_dayDatePicker.SelectedDate = DateTime.Now;

            this.productplan_insert_dayDatePicker.SelectedDateChanged += (sender, e) =>
            {
                setday();
            };

            this.product_nameComboBox.SelectionChanged += (sender, e) =>
            {
                setday();
            };





        }

        public void Init()
        {
             this.product_quantityTextBox.IsEnabled = false;
             this.productplanTextBox.IsEnabled = false;
             this.productplan_codeTextBox.IsEnabled = false;
             this.productplan_insert_dayDatePicker.IsEnabled = false;
             
           
        }

        public void setday()
        {
          
            try
            {
                var lo =
             context.Location_table
                          .Where(kkk => kkk.location == this.locationComboBox.Text)
                          .SingleOrDefault();

                DateTime days = this.productplan_insert_dayDatePicker.SelectedDate.Value;

                if (this.productplan_insert_dayDatePicker.SelectedDate != null)
                {
                    this.productplan_sub_predict_dayDatePicker.SelectedDate = days.AddDays(Convert.ToInt16(lo.set_predict));
                    this.productplan_set_predict_dayDatePicker.SelectedDate = days.AddDays(Convert.ToInt16(lo.complete_predict));
                }
            }
            catch(DataServiceQueryException dqe)
            {

            }
 
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.productplanTextBox.Text != null)
                {

                    //var procount = context.productplan_put.ToList().Count();
                    var procount = context.productplan_put.ToList().Max(kkk=>kkk.id);

                   productplan_put insertProductplan = new productplan_put
                    {

                        id = procount+1,
                        location = this.locationComboBox.Text,
                        productplan = this.productplanTextBox.Text,
                        productplan_code = "F",
                        productplan_insert_day = this.productplan_insert_dayDatePicker.SelectedDate.Value,
                        productplan_set_predict_day = this.productplan_set_predict_dayDatePicker.SelectedDate.Value,
                        productplan_sub_predict_day = this.productplan_sub_predict_dayDatePicker.SelectedDate.Value,
                        product_name = this.product_nameComboBox.Text,
                        product_quantity = Convert.ToInt32(this.product_quantityTextBox.Text),
                        work_order_number = null//Convert.ToInt32(this.work_order_numberTextBox.Text)
                        };

                    context.AddToproductplan_put(insertProductplan);
                    context.SaveChanges();

                    DataServiceCollection<productplan_put> lstData = new DataServiceCollection<productplan_put>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            productplan_putDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.productplan_put);

                    MessageBox.Show(this.productplanTextBox.Text + "(이)가 입력되었습니다.", "입력완료 메세지");

                    Reset();
                }
                else
                {
                    MessageBox.Show("입력하시오");
                }
            }
            catch (DataServiceRequestException dataServiceRequestException)
            {
                MessageBox.Show("같은 값이 있네요~");
            }
          
        }

    
        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.locationComboBox.Text != null)
            {
             
                productplan_put selectItem = context.productplan_put
                                             .Where(pp => pp.id == deleteId.id)
                                             .SingleOrDefault<productplan_put>();


                selectItem.location = this.locationComboBox.Text;
                selectItem.productplan = this.productplanTextBox.Text;
                selectItem.productplan_code = this.productplan_codeTextBox.Text;
                selectItem.productplan_insert_day = this.productplan_insert_dayDatePicker.SelectedDate.Value;
                selectItem.productplan_set_predict_day = this.productplan_set_predict_dayDatePicker.SelectedDate.Value;
                selectItem.productplan_sub_predict_day = this.productplan_sub_predict_dayDatePicker.SelectedDate.Value;
                selectItem.product_name = this.product_nameComboBox.Text;
                selectItem.product_quantity = Convert.ToInt32(this.product_quantityTextBox.Text);
                selectItem.work_order_number = Convert.ToInt32(this.work_order_numberTextBox.Text);



                if (selectItem != null)
                {
                    context.UpdateObject(selectItem);
                    context.SaveChanges();

                    DataServiceCollection<productplan_put> lstData = new DataServiceCollection<productplan_put>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                           productplan_putDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.productplan_put);

                    MessageBox.Show(selectItem.productplan + "의 수정이 완료 되었습니다.", "수정완료 메세지");

                    Reset();

                }
            }

        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            //var idx = this.location_tableDataGrid.SelectedIndex + 1;
            if (this.productplanTextBox.Text != null)
            {
                
                
                //MessageBox.Show(delete);
               productplan_put selectItem = context.productplan_put
                                            .Where(pp => pp.id == deleteId.id)
                                            .SingleOrDefault<productplan_put>();

                if (selectItem != null)
                {
                    context.DeleteObject(selectItem);

                    context.SaveChanges();

                    DataServiceCollection<productplan_put> lstData = new DataServiceCollection<productplan_put>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            productplan_putDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.productplan_put);

                    MessageBox.Show(selectItem.productplan + "(이)가 삭제되었습니다.", "삭제완료 메세지");

                    Reset();

                }
                else
                {
                    MessageBox.Show("데이터가 없네요. 삭제 할 데이터를 입력하시오");
                }
            }
            else
            {
                MessageBox.Show("삭제 할 생산계획 data를 입력하시오");
                this.productplanTextBox.Focus();
            }
        }

        public void Reset()
        {
            this.locationComboBox.Text = "";
            this.productplanTextBox.Text = "";
           this.productplan_codeTextBox.Text = "";
            this.productplan_insert_dayDatePicker.SelectedDate = DateTime.Now;
            this.productplan_set_predict_dayDatePicker.SelectedDate = DateTime.Now;
           this.productplan_sub_predict_dayDatePicker.SelectedDate = DateTime.Now;
            this.product_nameComboBox.Text = "";
            this.product_quantityTextBox.Text = "";
            this.work_order_numberTextBox.Text = "";
        }

       
    }
}
